<?php

/**
 * 俱乐部相关
 */
class messagePmModel extends paomeBaseModel
{

    public function __construct(& $base)
    {
        parent::__construct($base);

        $this->table = 'r_pm_message';
        //P1 P2申请者昵称 P3 P4 战队昵称
        $this->descType = array(
            "P1"=>"%s 申请加入您的战队",
            "P2"=>"%s 已退出您的战队",
            "P3"=>"您已加入 %s",
            "P4"=>"您被移出 %s"
        );
    }

    /**
     * 消息信息
     */
    public function getMsgInfo($mid,$fields='*') {
        return  $this->paomeDb->getRow("select $fields from r_pm_message where id=$mid");
    }

    /**
     * 发送消息
     * $param $descType 消息类型：P1:申请加入您的战队P2:已退出您的战队 P3:您已加入 P4 您被移除战队
     * 成功返回：array(1) {
            ["code"]=>
            int(200)
        }
     */
    public function sendMsg(array $params) {
        $mid = $params['mid'];
        $uid = $params['uid'];
        $toUid = $params['toUid'];
        $descType = $params['descType'];
        $sourceId = $params['sourceId'];
        $pushContentDesc = $this->getPushContent(array(
            'descType'=>$descType,
            'sourceId'=>$sourceId
        ));
        //发消息
        $extra = base64_encode(json_encode(array(
            "is_jogger"=>1,
            "id" => $mid,
            "user_id" => $sourceId,
            "user_name" => '跑么杯小助手',
            "user_header" => $this->base->config['host'].'/static/images/share/pao.png',
            "content" => $pushContentDesc,
            "target_url"=>$this->base->config['host'].'/paome_webIndex/paoNews'
        )));
        return $this->sendPaomeMsg(array('target_id'=>$toUid,'extra'=>$extra,'content'=>''));
    }

    /**
     * 内容渲染
     */
    public function getPushContent(array $params) {
        $descType = $params['descType'];
        $sourceId = $params['sourceId'];
        $pushContentDesc ='';
        /*内容渲染*/
        switch($descType) {
            case 'P1':
            case 'P2':
                $nickname = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $sourceId,'nickname');
                $pushContentDesc = vsprintf($this->descType[$descType],array($nickname));
                break;
            case 'P3':
            case 'P4':
                $arrClubInfo = $this->base->load('paome/paomeClub')->getClubInfo($sourceId, 'title');
                $nickname = $arrClubInfo ? $arrClubInfo['title'] : '';
                $pushContentDesc = vsprintf($this->descType[$descType],array($nickname));
                break;
        }
        return $pushContentDesc;
    }

    /**发消息
     * @param $data
     */
    public function sendPaomeMsg($data) {
        $fromUserId = "14413";//test
        //$fromUserId = "12481157";//线上账号
        $toUserId = array($data['target_id']);
        $objectName = "RC:TxtMsg";
        $content = '{"content":"'.$data['content'].'","extra":"'.$data['extra'].'"}';
        $pushContent = "";
        $pushData = "1";
        return $this->base->load('message')->messagePublish($fromUserId,$toUserId ,$objectName,$content,$pushContent,$pushData);
    }

    /**
     * 写入消息
     */
    public function addPaomeMsg(array $params) {
        $data['from_uid'] = $params['fromUid'];
        $data['to_uid'] = $params['toUid'];
        $data['club_id'] = $params['clubId'];
        $data['source_id'] = $params['sourceId'];
        $data['desc_type'] = $params['descType'];
        $data['status'] = 0;
        $data['create_time'] = time();
        $result = $this->paomeDb->insertRow('r_pm_message',$data,mysqldb::INSERT_COMMON);
        return $result;
    }

    /**
     * 更新消息
     * $param $id 消息ID
     * $param $status 变更状态
     */
    public function modifyPaomeMsg($id, $status) {
        return $this->paomeDb->update("update r_pm_message set status=$status where id=$id");
    }

    /**
     * 消息列表
     */
    public function getMsgList(array $params) {
        $uid = $params['uid'];
        $mid = $params['mid'];
        $offset = $params['offset'];
        $this->total = $this->paomeDb->getOne("select count(1) from r_pm_message where to_uid=$uid and status<>-5");
        $list = [];
        if ($this->total > 0) {
            if ($mid > 0) {
                $list = $this->paomeDb->getAll("select * from r_pm_message where id<$mid and to_uid=$uid and status<>-5 order by id desc limit $offset");
            } else {
                $list = $this->paomeDb->getAll("select * from r_pm_message where to_uid=$uid and status<>-5 order by id desc limit $offset");
            }
        }
        return $list;
    }

    /**
     * 用户消息已读 未读数量
     * @param $uid
     * @param int $type 0 未读 1 已读
     */
    public function getMsgNum($uid, $type=0) {
        $where = "to_uid=$uid";
        if ($type == 1) {
            $where .= " and is_read=1";
        } else {
            $where .= " and is_read=0";
        }
        return $this->paomeDb->getOne("select count(1) from r_pm_message where $where");
    }

    /**
     * 更新消息已读
     */
    public function setMsgIsRead($uid) {
        return $this->paomeDb->update("update r_pm_message set is_read= 1 where to_uid=$uid");
    }


    /**
     * 发送获取证书消息
     */
    public function sendMsgForCertificate() {

    }





}